﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmbenhnhan : Form
    {
        public static string _abc;
        public frmbenhnhan()
        {
            InitializeComponent();
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

            TV_Tonghop.Themcot("MA_BN", "Mã Bệnh Nhân", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("HOTEN_BN", "Họ Tên Bệnh Nhân", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Giới", "Giới Tính ", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("NAMSINH_BN", "Năm Sinh ", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("DIACHI_BN", "Địa Chỉ ", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("SDT_BN", "Số Điện Thoại", dt_BenhNhan, DataGridViewAutoSizeColumnMode.Fill);

            col = new DataGridViewTextBoxColumn();
            col.Visible = false;
            col.DataPropertyName = "GIOITINH_BN";
            col.Name = col.DataPropertyName;
            dt_BenhNhan.Columns.Add(col);
        }


        //ham ket noi co so du lieu
        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");

        }

        private void frmbenhnhan_Load(object sender, EventArgs e)
        {

            /////////////////////////
            SqlConnection ketnoi = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("HienThiBenhNhan", ketnoi); //ket noi den co so du lieu de goi ham da khai bao trong StoredProcedure
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dt_BenhNhan.DataSource = dt;// do du lieu vao DataGridView dt_BenhNhan
        }

        private void Thêm_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemBenhNhan", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@mabn", SqlDbType.NChar).Value = maBN.Text;
            cmd.Parameters.Add("@hoten_bn", SqlDbType.NVarChar).Value = tenBN.Text;
            cmd.Parameters.Add("@gioitinh_bn", SqlDbType.Bit).Value = rdbNam.Checked;
            cmd.Parameters.Add("@namsinh_bn", SqlDbType.Date).Value = Convert.ToDateTime(dtnamsinh.Text); // mặc định thì dtnamsinh.text là kiểu string
            cmd.Parameters.Add("@diachi_bn", SqlDbType.NVarChar).Value = diachi.Text;// mình phải chuyển nó về kiểu date
            cmd.Parameters.Add("@sdt_bn", SqlDbType.VarChar).Value = sodienthoai.Text;
            cmd.ExecuteNonQuery();
            frmbenhnhan_Load(sender, e);
        }



        private void bt_Sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatBenhNhan", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@mabn", SqlDbType.NChar).Value = maBN.Text;
            cmd.Parameters.Add("@hoten_bn", SqlDbType.NVarChar).Value = tenBN.Text;
            cmd.Parameters.Add("@gioitinh_bn", SqlDbType.Bit).Value = rdbNam.Checked;
            cmd.Parameters.Add("@namsinh_bn", SqlDbType.Date).Value = Convert.ToDateTime(dtnamsinh.Text);
            cmd.Parameters.Add("@diachi_bn", SqlDbType.NVarChar).Value = diachi.Text;
            cmd.Parameters.Add("@sdt_bn", SqlDbType.VarChar).Value = sodienthoai.Text;
            cmd.ExecuteNonQuery();
            frmbenhnhan_Load(sender, e);
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaBenhNhan", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;// kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@mabn", SqlDbType.Char).Value = maBN.Text;
            cmd.ExecuteNonQuery();
            frmbenhnhan_Load(sender, e);
        }

        int dong;
        private void dt_BenhNhan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            maBN.Text = dt_BenhNhan.Rows[dong].Cells[0].Value.ToString();
            tenBN.Text = dt_BenhNhan.Rows[dong].Cells[1].Value.ToString();
            rdbNam.Checked = (bool)dt_BenhNhan.Rows[dong].Cells["Gioitinh_BN"].Value;
            rdbNu.Checked = !rdbNam.Checked;
            dtnamsinh.Text = dt_BenhNhan.Rows[dong].Cells["NAMSINH_BN"].Value.ToString();
            sodienthoai.Text = dt_BenhNhan.Rows[dong].Cells[5].Value.ToString();
            diachi.Text = dt_BenhNhan.Rows[dong].Cells[4].Value.ToString();
        }

        private void sodienthoai_TextChanged(object sender, EventArgs e)
        {

        }

        private void maBN_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void grp_benhnhan_Click(object sender, EventArgs e)
        {

        }




    }
}
